#include "common.h"

class Solution
{
public:
    int minSubArrayLen(int target, vector<int> &nums)
    {
        int sz = nums.size();
        int minlen = sz + 1;
        int left = 0, right = 0;
        int sum = 0;
        while (right < sz && left < sz)
        {
            sum += nums[right];
            while (sum >= target)
            {
                int len = right - left + 1;
                minlen = minlen > len ? len : minlen;
                sum -= nums[left++];
            }
            ++right;
        }
        return minlen == sz + 1 ? 0 : minlen;
    }
};

class Solution
{
public:
    int minSubArrayLen(int target, vector<int> &nums)
    {
        int left = 0, right = 0;
        int sz = nums.size();
        int sum = 0;
        int minLen = sz + 1;
        while (right < sz)
        {
            sum += nums[right];
            while (sum >= target)
            {
                int len = right - left + 1;
                minLen = len > minLen ? minLen : len;
                sum -= nums[left++];
            }
            ++right;
        }
        return minLen == sz + 1 ? 0 : minLen;
    }
};